package it.polimi.TravelDream.web.beans;

import it.polimi.TravelDream.ejb.dtos.RoomDTO;
import it.polimi.TravelDream.ejb.searchManager.RoomMgr;
import it.polimi.TravelDream.web.utilities.RoomContainer;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "RoomMgr")
@SessionScoped
public class RoomMgrBean {
	@EJB
	private RoomMgr roomMgr;
	private List <RoomContainer> currentList;
	private List <RoomDTO> dtosList;
	


	public RoomMgrBean(){
		this.currentList = new ArrayList<RoomContainer>();
		this.dtosList = new ArrayList<RoomDTO>();
	}
	
	public String search(){
		roomMgr.search();
		this.dtosList = roomMgr.getRoomResultList();
		createCurrentList();
		return "search room results?faces-redirect=true";
	}
	
	public String searchAll(){
		roomMgr.searchAll();
		this.dtosList = roomMgr.getRoomResultList();
		createCurrentList();
		return "search room results?faces-redirect=true";
	}

	public RoomMgr getRoomMgr() {
		return roomMgr;
	}

	public void setRoomMgr(RoomMgr roomMgr) {
		this.roomMgr = roomMgr;
	}

	public List<RoomContainer> getCurrentList() {
		return currentList;
	}

	public void setCurrentList(List<RoomContainer> currentList) {
		this.currentList = currentList;
	}
	
	private void createCurrentList(){
		for(String hname:this.roomMgr.getHotelName()){
			RoomContainer rc = new RoomContainer();
			rc.setHotelName(hname);
				for(RoomDTO rdto:this.dtosList){
					if(rdto.getHotelName()==hname){
						rc.addElementToRoomList(rdto);
					}
				}
			this.currentList.add(rc);	
		}
	}
}
